package org.example

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}


object SparkStreamingData2_1 {
  def main(args: Array[String]): Unit = {
//    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
//    // 5秒统计一次
//    val ssc = new StreamingContext()(sparkConf, Seconds(5))
//    //  监听并读取数据(一行行)
//    val lines = ssc.socketTextStream("192.168.64.129", 8888)
//    //数据处理
//    val dStream: ReceiverInputDStream[String] = ssc.socketTextStream("192.168.64.129",9999)
//    val lines: DStream[String] = dStream.flatMap(_.split(","))
//    val wordDStream: DStream[(String,Int)] = lines.map((_,1))
//    val result = wordDStream.reduceByKey(_+_)
//    //数据保存
//    result.print()
//    ssc.start()
//    ssc.awaitTermination()
//    ssc.stop()
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
    // 5秒统计一次
    val ssc = new StreamingContext(sparkConf, Seconds(5))
    //数据处理
    val dStream: ReceiverInputDStream[String] = ssc.socketTextStream("172.16.104.23",9999)
    //  词频统计
    val lines: DStream[String] = dStream.flatMap(_.split(" "))
    val wordDStream: DStream[(String,Int)] = lines.map((_,1))
    val result = wordDStream.reduceByKey(_+_)
    //数据保存
    result.print()
    ssc.start()
    ssc.awaitTermination()
  }
}
